<?php
class YicmsAction extends Action {
	public function index(){
		/**
		 * 使用前请先将此文件上传到您的网站根目录
		 * 为了网站看起来更真实，请先行将网站批量添加一些用户，使用SQL添加用户很简单
		 */
		set_time_limit(0);
		if (!$_POST) {
			$cat = M('Nav')->field('id,pid,name')->where("type=1 AND pid=1")->select();				
			foreach ($cat as $key => $value){
				echo "<li row='{$value['id']}'>{$value['name']}</li>\r\n";
			}
			exit();
		}
		/**
		 * 检测必需的字段，不能为空
		 */
		if (!$_POST['content'] || !$_POST['pid']) {
			exit('field is empty');
		}
		/**
		 *插入数据库
		 */
		$data['uid'] = $uid = mt_rand(1,2);
		$data['title'] = stripslashes(strip_tags($_POST['title']));
		$data['content'] = stripslashes(strip_tags($_POST['content']));
		$data['time'] = $_SERVER['REQUEST_TIME'];
		$data['pid'] = $_POST['pid'];
		$data['state'] = '1';
		if(trim($_POST['tag'])) $data['tag'] = stripslashes($_POST['tag']);
		$id = M('News')->data($data)->add();
		M('User')->where("uid = $uid")->setInc('articles');
		
		if(trim($_POST['arcontent'])){
			$arcontent = explode('|||',stripslashes(strip_tags($_POST['arcontent'])));
			foreach($arcontent as $value){
				$reply['arid'] = $uid = mt_rand(1,2);
				$reply['artime'] = $_SERVER['REQUEST_TIME'];
				$reply['ip']   = ip2long($_SERVER['REMOTE_ADDR']);
				$reply['state']= '1';
				$reply['pid']  = $id;
				$reply['arcontent'] = $value;
				M('Reply')->data($reply)->add();
				M('User')->where("uid = $uid")->setInc('comments');
				M('News')->where("id = $id")->setInc('reply');
			}
		}
	}
}
?>